package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SourceOfLeadCustomersMapper;
import com.ruoyi.system.domain.SourceOfLeadCustomers;
import com.ruoyi.system.service.ISourceOfLeadCustomersService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 线索客户来源Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-09
 */
@Service
public class SourceOfLeadCustomersServiceImpl implements ISourceOfLeadCustomersService
{
    @Autowired
    private SourceOfLeadCustomersMapper sourceOfLeadCustomersMapper;

    /**
     * 查询线索客户来源
     *
     * @param id 线索客户来源主键
     * @return 线索客户来源
     */
    @Override
    public SourceOfLeadCustomers selectSourceOfLeadCustomersById(Long id)
    {
        return sourceOfLeadCustomersMapper.selectSourceOfLeadCustomersById(id);
    }

    /**
     * 根据线索客户名称查询线索客户来源
     * @param name
     * @return
     */
    @Override
    public SourceOfLeadCustomers selectSourceOfLeadCustomersByName(String name) {
        return sourceOfLeadCustomersMapper.selectSourceOfLeadCustomersByName(name);
    }

    /**
     * 查询线索客户来源列表
     *
     * @param sourceOfLeadCustomers 线索客户来源
     * @return 线索客户来源
     */
    @Override
    public List<SourceOfLeadCustomers> selectSourceOfLeadCustomersList(SourceOfLeadCustomers sourceOfLeadCustomers)
    {
        return sourceOfLeadCustomersMapper.selectSourceOfLeadCustomersList(sourceOfLeadCustomers);
    }

    /**
     * 新增线索客户来源
     *
     * @param sourceOfLeadCustomers 线索客户来源
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSourceOfLeadCustomers(SourceOfLeadCustomers sourceOfLeadCustomers)
    {
        int status = 0;
        try {
            //新增线索客户来源判空方法
            emptyInsertSourceOfLeadCustomers(sourceOfLeadCustomers);
            status = sourceOfLeadCustomersMapper.insertSourceOfLeadCustomers(sourceOfLeadCustomers);
        }
        catch (Exception e){
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new RuntimeException(e.getMessage());
        }
        return status;
    }

    /**
     * 新增线索客户来源判空方法
     * @param sourceOfLeadCustomers
     */
    @Override
    public void emptyInsertSourceOfLeadCustomers(SourceOfLeadCustomers sourceOfLeadCustomers) {
        //线索客户来源名称
        if (StringUtils.isEmpty(sourceOfLeadCustomers.getName())){
            throw new RuntimeException("线索客户来源名称未填写");
        }
        //查询线索客户(判断是否有相同名称
        SourceOfLeadCustomers querySourceOfLeadCustomers = this.selectSourceOfLeadCustomersByName(sourceOfLeadCustomers.getName());
        if (null != querySourceOfLeadCustomers){
            throw new RuntimeException("当前线索客户来源已存在，请重新填写名称");
        }
    }

    /**
     * 修改线索客户来源
     *
     * @param sourceOfLeadCustomers 线索客户来源
     * @return 结果
     */
    @Override
    public int updateSourceOfLeadCustomers(SourceOfLeadCustomers sourceOfLeadCustomers)
    {
        return sourceOfLeadCustomersMapper.updateSourceOfLeadCustomers(sourceOfLeadCustomers);
    }

    /**
     * 批量删除线索客户来源
     *
     * @param ids 需要删除的线索客户来源主键
     * @return 结果
     */
    @Override
    public int deleteSourceOfLeadCustomersByIds(Long[] ids)
    {
        return sourceOfLeadCustomersMapper.deleteSourceOfLeadCustomersByIds(ids);
    }

    /**
     * 删除线索客户来源信息
     *
     * @param id 线索客户来源主键
     * @return 结果
     */
    @Override
    public int deleteSourceOfLeadCustomersById(Long id)
    {
        return sourceOfLeadCustomersMapper.deleteSourceOfLeadCustomersById(id);
    }
}